package com.vitabuybuy.tv.ui.tencent.chat

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.dto.MessageEvent
import com.tencent.imsdk.TIMConversationType
import com.tencent.imsdk.TIMManager
import com.tencent.qcloud.tim.uikit.component.TitleBarLayout
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationLayout
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListLayout
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationManagerKit
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationInfo
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityChatListBinding
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.fragment_cart.*
import org.greenrobot.eventbus.EventBus
import java.util.*

class MessageListActivity : BaseActivity<ChatViewModel, ActivityChatListBinding>() {
    private val listid: MutableList<String> = ArrayList()

    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_chat_list

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.message_list))
        // 初始化聊天面板
        mBinding.conversationLayout.run {
            this.initDefault()
            this.titleBar.visibility = View.GONE
        }
    }

    override fun onResume() {
        super.onResume()
        //zxy 获取会话消息列表
        val conversationLists = TIMManager.getInstance().conversationList
        if (conversationLists.isEmpty()) {
            mBinding.statusView.showEmpty()
        } else {
            mBinding.statusView.showContent()
            //zxy 更新本地数据
            for (i in conversationLists.indices) {
                if (conversationLists[i].type == TIMConversationType.C2C) {
                    listid.add(conversationLists[i].peer)
                }
            }
            ZxyTimUtls.getInstance().updataLocationData(listid)
        }
        mBinding.refresh.onLoad(-1)
    }

    override fun eventHandling() {
        //zxy 进入聊天
        mBinding.conversationLayout.conversationList.apply {
            setOnItemClickListener { _: View?, _: Int, messageInfo: ConversationInfo? ->
                val chatInfo = ChatInfo().apply {
                    type = TIMConversationType.C2C
                    id = messageInfo?.id
                    chatName = messageInfo?.title
                }
                ChatActivity.openActivity(mContext, chatInfo)
            }
            setOnItemDeleteClickListener { _, position, messageInfo ->
                AlertDialog.Builder(this@MessageListActivity)
                    .setTitle(getString(R.string.logout))
                    .setMessage(getString(R.string.are_you_sure_delete_conversation))
                    .setCancelable(true)
                    .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                        ConversationManagerKit.getInstance()
                            .deleteConversation(position, messageInfo)
                        mBinding.conversationLayout.removeConversationInfo(position)
                    }
                    .setNegativeButton(getString(R.string.cancel)) { _, _ -> }.create().show()
            }
        }
        mBinding.refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                onResume()
            }
        })
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, MessageListActivity::class.java)
        }
    }
}